package ift605.testDynamicServer.client;

import ift605.testDynamicServer.server.MyServerPublicInterface;
import ift605.testDynamicServer.server.Task;

import java.rmi.RemoteException;
import java.rmi.registry.LocateRegistry;
import java.rmi.registry.Registry;
import java.util.Scanner;

public class MyClient {

	public static void main(String[] args) {
		MyClient client = new MyClient();
		client.doSomething();
	}

	private void doSomething() {
		MyServerPublicInterface proxy = null;
	    Scanner in = new Scanner(System.in);
	    System.out.println("Enter ipAddr of RMI Registry");
		String ipAddr = in.nextLine();
	    
		if(ipAddr == null || ipAddr.isEmpty())
		{
			ipAddr = "localhost";
		}
		
	    try {
	    	Registry registry = LocateRegistry.getRegistry(ipAddr);
			proxy = (MyServerPublicInterface) registry.lookup("MyServer");
		} catch (Exception e) {
			e.printStackTrace();
			return;
		}
	    
	    Task aTask = null;

	    do
	    {
	    	aTask = GetTask(in);
	    	
	    	if(aTask != null)
	    	{		
	    		try
	    		{
	    			proxy.execute(aTask);
	    		}
	    		catch(Exception e)
	    		{
	    			e.printStackTrace();
	    		}
			}
	    }
	    while(aTask != null);
		
		in.close();
	}
	
	private Task GetTask(Scanner in)
	{
		Task task = null;
		{
			System.out.println("Choose command :");
			System.out.println("(1) : syso(6)");
			System.out.println("(2) : syso(Poisson)");
			System.out.println("(3) : Quit");
			
			int reading= in.nextInt();

			switch(reading)
			{
			case 1:
			{
				task = new SixSysoTask();
				break;
			}
			case 2:
			{
				task = new PoissonSysoTask();
				break;
			}
			case 3:
			{
				break;
			}
			default:
				break;
			}
		}
		
		return task;
	}
}